(function() {
	var $ = require(L('$')), _ = require(L('_')), tibar = require('tibar');
	var retailer = require(L('retailer'));

	exports.scan = function() {
		// $.mask();
		if ($.isEnableLocationService()) {
			$.gps().getCurrentPosition(function(currentLocationInfo) {
				if (currentLocationInfo === null)
					OpenLocationServiceDialog();
				else
					ScanForBarcode();
			});
		} else
			OpenLocationServiceDialog();
	};

	function OpenLocationServiceDialog(callBack) {
		$.a(L('location_service_content'), L('location_service_title'), ['Skip'], function(e) {
			ScanForBarcode();
		});
	};

	function ScanForBarcode() {
		tibar && tibar.scan({
			configure : {
				classType : "ZBarReaderViewController", //ZBarReaderViewController, ZBarReaderController
				sourceType : "Camera", //Library(C), Camera(VC), Album(C)
				cameraMode : "Default", //Default, Sampling, Sequence
				config : {
					"showsCameraControls" : true, //(VC)
					"showsZBarControls" : true,
					"tracksSymbols" : true//the tracking rectangle that highlights barcodes
				},
				custom : {
					//"scanCrop":'',
					//"CFG_X_DENSITY":'',
					//"CFG_Y_DENSITY":'',
					//"continuous":''
				},
				symbol : {
					"QR-Code" : true,
					"CODE-128" : true,
					"CODE-39" : true,
					"I25" : true,
					"DataBar" : true,
					"DataBar-Exp" : true,
					"EAN-13" : true,
					"EAN-8" : true,
					"UPC-A" : true,
					"UPC-E" : true,
					"ISBN-13" : true,
					"ISBN-10" : true,
					"PDF417" : true
				}
			},
			success : function(data) {
				data && data.barcode && BarcodeScanHandler(data.barcode);
			},
			error : function() {
			},
			cancel : function() {
				$.is_debug() && BarcodeScanHandler(L('debug_barcode'));
			}
		});
		// $.mask(false);
	};
	function BarcodeScanHandler(barcode) {
		$.webservice().getProductVariantByBarcode(barcode, function(responseText) {
			if (responseText) {
				var productVariantResponse = JSON.parse(responseText);
				if (productVariantResponse && productVariantResponse.ProductVariantInfo) {
					var productVariantInfoList = productVariantResponse.ProductVariantInfo;
					var productVariantInfoLength = productVariantInfoList.length;
					if (productVariantInfoLength === 1) {
						var alertDialog = Ti.UI.createAlertDialog({
							title : L('notification_title'),
							message : L('notification_message') + productVariantInfoList[0].FullName,
							buttonNames : [L('ok_button'), L('cancel_button')],
							cancel : 1,
							data : productVariantInfoList[0]
						});
						alertDialog.addEventListener('click', function(e) {
							if (e.index == 0)
								$.openProductDetails(e.source.data.ProductVariantID, e.source.data.RetailerStoreID, false, true);
						});
						alertDialog.show();
					} else if (productVariantInfoLength > 1) {
						retailer.showRetailerOptionsDialog(productVariantInfoList);
					} else {
						//$.a(L('barcode_not_existed_message'), L('barcode_not_existed_title'));
						AskForCreateUGD(barcode);
					}
				} else {
					//$.a(L('barcode_not_existed_message'), L('barcode_not_existed_title'));
					AskForCreateUGD(barcode);
				}
			}
		});
	};

	function AskForCreateUGD(missingBarcode) {
		$.askFor(L('barcode_not_existed_create_ugd'), 'Barcode error', ['YES', 'NO'], 1, function(e) {
			if (!e)
				return;
			if ($.is_debug()) {
				$.photo().select(function(e) {
					//$.win('ugd/preview').update(e, missingBarcode);
					$.win('ugd/form').create(e, missingBarcode);
				});
				return;
			}
			$.photo().take(function(e) {
				//$.win('ugd/preview').update(e, missingBarcode);
				$.win('ugd/form').create(e, missingBarcode);
			});
		});
	};

})();
